package cn.iocoder.yudao.module.system.controller.admin.standard;

import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.module.system.controller.admin.standard.vo.StandardPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.standard.vo.StandardRespVO;
import cn.iocoder.yudao.module.system.controller.admin.standard.vo.StandardSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.standard.StandardDO;
import cn.iocoder.yudao.module.system.service.standard.StandardService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 存储健康指标基准值，上下限通过计算规则动态生成")
@RestController
@RequestMapping("/health/standard")
@Validated
public class StandardController {

    @Resource
    private StandardService standardService;

    @PostMapping("/create")
    @Operation(summary = "创建存储健康指标基准值，上下限通过计算规则动态生成")
    @DataPermission(enable = false) // 关闭数据权限，避免只查看自己时，查询不到部门。
    public CommonResult<Long> createStandard(@Valid @RequestBody StandardSaveReqVO createReqVO) {
        return success(standardService.createStandard(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新存储健康指标基准值，上下限通过计算规则动态生成")
    @PreAuthorize("@ss.hasPermission('health:standard:update')")
    public CommonResult<Boolean> updateStandard(@Valid @RequestBody StandardSaveReqVO updateReqVO) {
        standardService.updateStandard(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除存储健康指标基准值，上下限通过计算规则动态生成")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('health:standard:delete')")
    public CommonResult<Boolean> deleteStandard(@RequestParam("id") Long id) {
        standardService.deleteStandard(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得存储健康指标基准值，上下限通过计算规则动态生成")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @DataPermission(enable = false) // 关闭数据权限，避免只查看自己时，查询不到部门。
    public CommonResult<StandardRespVO> getStandard(@RequestParam("id") Long id) {
        StandardDO standard = standardService.getStandard(id);
        return success(BeanUtils.toBean(standard, StandardRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得存储健康指标基准值，上下限通过计算规则动态生成分页")
    @PreAuthorize("@ss.hasPermission('health:standard:query')")
    public CommonResult<PageResult<StandardRespVO>> getStandardPage(@Valid StandardPageReqVO pageReqVO) {
        PageResult<StandardDO> pageResult = standardService.getStandardPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StandardRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出存储健康指标基准值，上下限通过计算规则动态生成 Excel")
    @PreAuthorize("@ss.hasPermission('health:standard:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStandardExcel(@Valid StandardPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StandardDO> list = standardService.getStandardPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "存储健康指标基准值，上下限通过计算规则动态生成.xls", "数据", StandardRespVO.class,
                        BeanUtils.toBean(list, StandardRespVO.class));
    }

}